/**
  mutex.h - Created by Timothy Morey on 3/20/2012.

  This file defines a cross-platform mutex.
*/

#ifndef __MUTEX_H
#define __MUTEX_H

#ifdef __WINDOWS__
#include <Windows.h>
#else
#include <pthread.h>
#endif

#include "libnvn.h"

/**
  The Mutex structure provides a platform independent thread mutex.
*/
typedef struct
{
#ifdef __WINDOWS__
  HANDLE Mutex;           // Windows handle to a mutex object.
#else
  pthread_mutex_t Mutex;  // A handle to a pthread_mutex.
#endif
} Mutex;

/**
  Initializes a mutex that is initially unlocked.

  @param A pointer to the mutex structure that will be initialized.

  @return 1 if the operation is successful, and 0 if an error occurs.
*/
EXPORT int InitMutex(Mutex* mutex);

/**
  Destroys a mutex previously created with a call to InitMutex.  Note that this
  function does not attempt to free the memory for mutex.

  @param mutex The mutex that will be destroyed.

  @return 1 if the operation is successful, and 0 if an error occurs.
*/
EXPORT int DestroyMutex(Mutex* mutex);

/**
  Gets a lock for the mutex.  This function will block until the lock is
  achieved.

  @param The desired mutex.

  @return 1 if the operation is successful, and 0 if an error occurs.
*/
EXPORT int GetLock(Mutex mutex);

/**
  Releases the mutex.

  @param The mutex to release.

  @return 1 if the operation is successful, and 0 if an error occurs.
*/
EXPORT int ReleaseLock(Mutex mutex);

#endif